package com.example.demo.kafka;

import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

/**
 * 类描述:
 *
 * @author jianweiwang
 * @version v1.0
 * @link wjw@choicesoft.com.cn
 * @date 2018/12/12 18:29
 **/
@EnableAutoConfiguration
@RestController
@Slf4j
public class KafkaController {
    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;

    private void send(String key, String data) {
        kafkaTemplate.send("wjwei", key, data);
    }

    @RequestMapping("/kafka")
    public String testKafka() {
        int iMax = 6;
        for (int i = 1; i < iMax; i++) {
            send("key_" + Math.random()*100, "data_" + UUID.randomUUID().toString());
        }
        return "success";
    }
    public static void main(String[] args) {
        SpringApplication.run(KafkaController.class, args);
    }

//    @KafkaListener(topics = "wjwei")
//    public void receive(ConsumerRecord<?, ?> consumer) {
//        log.info("{} - {}:{}", consumer.topic(), consumer.key(), consumer.value());
//    }

}
